<?php

$db_obj = new db_obj();
$task_obj = new task_obj();
$task_phorum_obj = new task_phorum_obj();
$client_obj = new client_obj();
$user_obj = new user_obj();
$filter_obj = new filter_obj();

if (!isset($_SESSION['list_of_tasks_pager'])) {
    $_SESSION['list_of_tasks_pager'] = $GLOBALS['list_of_tasks_pager'];
}

if (!isset($_SESSION['list_of_tasks_order']) || (isset($_REQUEST['clear_tasks_order']) && ($_REQUEST['clear_tasks_order']==1))) {
    $_SESSION['list_of_tasks_order'] = $GLOBALS['list_of_tasks_order'];
}

if (isset($_REQUEST['clear_tasks_order']) && ($_REQUEST['clear_tasks_order']==1)) {
    $_SESSION['list_of_tasks_filter'] = array(
        'status' => ACTIVE_TASK
    );
}

if (isset($_SESSION['list_of_tasks_fulltext']) && !empty($_SESSION['list_of_tasks_fulltext']))
    $GLOBALS['template_data']['currentFulltext'] = $_SESSION['list_of_tasks_fulltext'];

/*if (isset($_SESSION['request']['client']) && !empty($_SESSION['request']['client'])) {
    $_SESSION['list_of_tasks_filter']['client'] = $_SESSION['request']['client'];
    unset($_SESSION['request']['client']);
}*/

$filter = array();

$currentFilter = array();

if (isset($_SESSION['list_of_tasks_filter'])) {
    $filter = $_SESSION['list_of_tasks_filter'];

    $result = $filter_obj->updateFilter($_SESSION['user']['user_id'], 'list_of_tasks_filter', $filter);
    if (error_obj::isError($result)) {
        error_obj::setError($result, UNEXCEPTED);
    }

    //if (isset($_SESSION['user']['clientFilter']) && !isset($filter['client'])){
    //    $filter['client'] = $_SESSION['user']['clientFilter'];
    //}
    
    foreach ($filter as $f => $v) {
        switch ($f) {
            case 'title':
                $currentFilter[$f] = $v;
                break;
            case 'client':
                $currentFilter[$f] = $client_obj->getClient($v, array('name'));
                $currentFilter[$f] = $currentFilter[$f][0];
                break;
            case 'creator':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'updater':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'responsible':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'worker':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'responsible_or_worker':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'added':
                $currentFilter[$f] = base_obj::convertDateTo($v, 'dd/mm/yyyy H:i:s', 'datetime');
                break;
            case 'updated':
                $currentFilter[$f] = base_obj::convertDateTo($v, 'dd/mm/yyyy H:i:s', 'datetime');
                break;
            case 'deadline':
                $currentFilter[$f] = base_obj::convertDateTo($v, 'dd/mm/yyyy', 'date');
                if ($currentFilter[$f] == MAX_DEADLINE)
                    $currentFilter[$f] = '00/00/0000';
                break;
            case 'type':
                $currentFilter[$f] = $task_obj->getTaskType($v);
                $currentFilter[$f] = $currentFilter[$f]['type_of_record'];
                break;
            case 'status':
                $currentFilter[$f] = $task_obj->getTaskStatus($v);
                $currentFilter[$f] = $currentFilter[$f]['stav'];
                break;
            case 'priority':
                $currentFilter[$f] = $task_obj->getTaskPriority($v);
                $currentFilter[$f] = $currentFilter[$f]['Priorita'] . '-' . $currentFilter[$f]['Popis'];
                break;
            case 'problem_type':
                if (!empty($v)) {
                    $currentFilter[$f] = $task_obj->getTaskTypeOfProblem($v);
                    $currentFilter[$f] = $currentFilter[$f]['problem'];
                } else {
                    $currentFilter[$f] = '-';
                }
                break;
            case 'consult':
                if ($v == 1)
                    $currentFilter[$f] = 'Yes';
                else
                    $currentFilter[$f] = 'No';
                break;
        }
    }
}

$GLOBALS['template_data']['currentFilter'] = $currentFilter;

if (!isset($_GET['page'])) {
    $_GET['page'] = 0;
}

$limit = ($_GET['page'] * $_SESSION['list_of_tasks_pager']) . ' ,' . $_SESSION['list_of_tasks_pager'];

if ($_SESSION['user']['profile']['userlevel'] == USER_TYPE_CLIENT) {
    $filter['clients'] = $_SESSION['user']['clients'];
}

if (isset($_SESSION['list_of_tasks_fulltext']) && !empty($_SESSION['list_of_tasks_fulltext'])){
    $PAGER_COUNT = $task_obj->getListOfTasksByFulltext($_SESSION['list_of_tasks_fulltext'], $filter, $_SESSION['list_of_tasks_order'], '', 'COUNT(*)');
    $tasks = $task_obj->getListOfTasksByFulltext($_SESSION['list_of_tasks_fulltext'], $filter, $_SESSION['list_of_tasks_order'], $limit);
} else {
    $PAGER_COUNT = $task_obj->getListOfTasks($filter, $_SESSION['list_of_tasks_order'], '', 'COUNT(*)');
    $tasks = $task_obj->getListOfTasks($filter, $_SESSION['list_of_tasks_order'], $limit);
}
$PAGER_COUNT = $PAGER_COUNT[0][0];
$GLOBALS['template_data']['total_count'] = $PAGER_COUNT;
$GLOBALS['template_data']['currentOrder'] = array(
    $_SESSION['list_of_tasks_order']['col'],
    $_SESSION['list_of_tasks_order']['order']
);

foreach ($tasks as $key => $task) {
    $tasks[$key]['client_info'] = $client_obj->getClient($task['client'], array('name'));
    $tasks[$key]['creator_info'] = $user_obj->getUserById($task['creator'], array('name', 'surname'));
    $tasks[$key]['deadline_format'] = base_obj::convertDateTo($task['deadline'], 'dd/mm/yyyy', 'date');
    if ($tasks[$key]['deadline_format'] == MAX_DEADLINE)
        $tasks[$key]['deadline_format'] = '00/00/0000';
    $tasks[$key]['created_format'] = base_obj::convertDateTo($task['added'], 'dd/mm/yyyy H:i:s', 'datetime');
    if (($task['updater'] == 0)) {
        $tasks[$key]['updater_isset'] = 0;
    } else {
        $tasks[$key]['updater_isset'] = 1;
        $tasks[$key]['updater_info'] = $user_obj->getUserById($task['updater'], array('name', 'surname'));
        $tasks[$key]['updated_format'] = base_obj::convertDateTo($task['updated'], 'dd/mm/yyyy H:i:s', 'datetime');
    }
    $tasks[$key]['responsible_info'] = $user_obj->getUserById($task['responsible'], array('name', 'surname'));
    if ($task['worker'] != -1)
        $tasks[$key]['worker_info'] = $user_obj->getUserById($task['worker'], array('name', 'surname'));
    $tasks[$key]['type_info'] = $task_obj->getTaskType($task['type']);
    $tasks[$key]['priority_info'] = $task_obj->getTaskPriority($task['priority']);
    $tasks[$key]['status_info'] = $task_obj->getTaskStatus($task['status']);
    if (!empty($task['problem_type']))
        $tasks[$key]['problem_type_info'] = $task_obj->getTaskTypeOfProblem($task['problem_type']);
    else {
        $tasks[$key]['problem_type_info']['problem'] = '-';
    }
    $tasks[$key]['posts_in_phorum'] = $task_phorum_obj->getPostsInPhorum(array("task_id" => $task['taskId']), '', '', array('COUNT(task_phorum_id)'));
    $tasks[$key]['posts_in_phorum'] = $tasks[$key]['posts_in_phorum'][0][0];
    $tasks[$key]['deadline_time'] = strtotime($task['deadline']);
    if ($task['status'] == 1) {
        if (date("dmY", strtotime($task['deadline']))==date("dmY")){
            $tasks[$key]['state'] = 2; // today is deadline
        } elseif (strtotime($task['deadline'])<time()){
            $tasks[$key]['state'] = 3; // it is after deadline
        } else {
            $tasks[$key]['state'] = 0;
            $tasks[$key]['difference'] = $difference = (strtotime($task['deadline']) - mktime(0, 0, 0, date("m"), date("d"), date("Y"))) / 24 / 3600;
            $limitToYellow = 2;
            $weekend = 0;
            if ($difference <= $limitToYellow + 2){
                for ($i=1; $i<=($limitToYellow + 2); $i++){
                    if ((date("w", strtotime($task['deadline'])-($i*24*3600))==0) && (date("w", strtotime($task['deadline'])-($i*24*3600))==6)){
                        $weekend++;
                    }    
                }
                $working_day = $difference - $weekend;
                $tasks[$key]['wd'] = $working_day;
                if ($working_day <= $limitToYellow)
                    $tasks[$key]['state'] = 1;
            }
        }
    } else {
        $tasks[$key]['state'] = 0;
    }
}


$GLOBALS['template_data']['tasks'] = $tasks;

$pager = $db_obj->pager($PAGER_COUNT, $_GET['page'], $_SESSION['list_of_tasks_pager']);

$GLOBALS['template_data']['pager'] = $pager;

$filter = array();
$filter['active'] = 1;
$filter['client'] = 11;
$GLOBALS['template_data']['responsibles'] = $user_obj->getUsers('name, surname, users_id', $filter);

$filter = array();
$filter['active'] = 0;
if ($_SESSION['user']['profile']['userlevel'] == USER_TYPE_CLIENT) {
    $filter['clients'] = $_SESSION['user']['clients'];
}
$GLOBALS['template_data']['clients'] = $client_obj->getAllClients('klient_id, name,  bad_payer', $filter, 'name ASC');

$GLOBALS['template_data']['statuses'] = $task_obj->getTaskStatuses("stav");
?>
